<!-- 配送商配送准入列表页-->
<template>
  <div class="channel-management-distribution-admittance-box">
    <el-container class="template-box">
      <!-- tab搜索 -->
      <tabs
        class="tabs-part"
        :active-index="tabIndex"
        :tabs-arr="indexTabsArr"
        @returnIndex="tabsHandle"
      />
      <!-- 搜索 -->
      <el-header height="auto">
        <SearchForm
          label-width="90px"
          :data="indexSearchData"
          @searchForm="searchForm"
          @resetForm="resetForm"
          :submit-loading="submitLoading"
        />
      </el-header>
      <!-- 列表 -->
      <el-editable
        v-loading="tableLoading"
        :data="dataList"
        height="400"
        :columns="indexTableData">
        <!-- 证照提示 - 医疗机构不需要提示 -->
        <!-- <template
          slot="default-enterpriseName"
          slot-scope="scope">
          <span>
            <el-tooltip
              v-if="scope.row.isNoExpire === 'Y'"
              effect="dark"
              :content="`有证照过期：${scope.row.enterpriseName}`"
              placement="top">
              <div
                class="custom-oneline"
                :style="{color: 'red'}">{{ scope.row.enterpriseName }}</div>
            </el-tooltip>
            <custom-tooltip
              v-else
              :value="scope.row.enterpriseName"/>
          </span>
        </template> -->
        <!-- 审核历史 -->
        <template
          slot="default-history"
          slot-scope="scope">
          <el-button
            size="small"
            @click.stop="lookHistory(scope.row)"
            type="text text-primary">查看</el-button>
        </template>
        <!-- 操作 -->
        <template
          slot="default-action"
          slot-scope="scope">
          <el-button
            v-if="scope.row.authState===1"
            size="small"
            @click.stop="edit(scope.row)"
            type="text text-primary"
          >编辑</el-button>
          <el-button
            size="small"
            @click.stop="toDetails(scope.row)"
            type="text text-primary">详情</el-button>
        </template>
      </el-editable>
      <!-- 分页模块 -->
      <el-pagination
        class="pagination"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="curPage"
        :page-sizes="PAGE_NUMBER"
        :page-size="pageSize"
        :layout="PAGINATION_LAYOUT"
        :total="totalElements"
      />
      <el-dialog
        title="审核历史"
        :visible.sync="isShowHistory">
        <el-editable
          v-loading="historyLoading"
          :data="historyDataList"
          height="400"
          :columns="historyTableData"
        />
        <span
          slot="footer"
          class="dialog-footer">
          <el-button
            type="primary"
            @click="isShowHistory = false">确 定</el-button>
        </span>
      </el-dialog>
    </el-container>
  </div>
</template>
<script>
import {
  indexSearchData,
  indexTableData,
  indexTabsArr,
  historyTableData
} from './const'
import { hasIn, isObject } from 'lodash'
import SearchForm from '@components/businesscomponents/search'
import ElEditable from '@components/el-editable'
import CustomTooltip from '@components/custom-tooltip'
import { PAGE_NUMBER, PAGINATION_LAYOUT } from '@const/common'
import tabs from '@components/tabs/index.vue'
import {
  findAuthSupplierHospitalList,
  authSupplierHospitalSelectcAcAuditRecord,
  authSupplierHospitalAuditingByHospital,
  commonPcAdmittanceTemporarydeleteAll
} from '@const/beforeSettleApi'
import { loadavg } from 'os'
export default {
  components: { ElEditable, SearchForm, tabs, CustomTooltip },
  data () {
    return {
      indexSearchData, // 搜索标题字段
      searchData: {}, // 搜索返回内容
      tableLoading: false, // 表格loading
      dataList: [], // 列表数据
      indexTableData, // 列表表头
      indexTabsArr, // tab字段
      submitLoading: false,

      isShowHistory: false, // 是否显示审核历史
      historyLoading: false, // 审核历史loading
      historyTableData, // 审核历史表头
      historyDataList: [], // 审核历史数据

      curPage: 1, // 当前页
      PAGE_NUMBER, // 页码数组
      PAGINATION_LAYOUT, // 页码功能布局
      pageSize: PAGE_NUMBER[0], // 一页的数量
      totalElements: 0, // 总共的条数
      tabIndex: 5, // 页面默认全部
      authState: -1 // 传给后台的Tab类型
    }
  },
  created () {
    this.getTableList()
    this._initGlobalBtn()
  },
  methods: {
    // 获取列表
    getTableList () {
      this.tableLoading = true
      this.$axios({
        method: 'post',
        url: findAuthSupplierHospitalList,
        data: {
          type: 2, // 院端传1 配送商 2
          enterpriseName: this.searchData.enterpriseName
            ? this.searchData.enterpriseName
            : '', // 查医疗机构
          beginDate: this.searchData.beginDate
            ? this.searchData.beginDate
            : null,
          endDate: this.searchData.endDate ? this.searchData.endDate : null,
          authState: this.authState,
          page: this.curPage,
          size: this.pageSize,
          supplierId: this.$store.state.companyId
        }
      })
        .then(res => {
          if (hasIn(res, ['data', 'payload']) && isObject(res.data.payload)) {
            this.dataList =
              res.data.payload.authSupplierHospitalListByParam.content || []
            this.totalElements = res.data.payload.totalElements || 0
          } else {
            this.dataList = []
            this.totalElements = 0
          }
        })
        .finally(() => {
          this.tableLoading = false
        })
    },
    // tabs切换处理事件
    tabsHandle (index) {
      this.authState = this.indexTabsArr[index].val
      this.curPage = 1
      this.getTableList()
    },
    // 跳转详情
    toDetails (row) {
      this.deleteAll(row.enterpriseId)
      sessionStorage.setItem('detailInfo', JSON.stringify(row))
      this.$router.push({
        path: '/distributorSide/distributionAdmittance/detail',
        query: {
          id: row.id, // 'af581b847ba542bfbbc7560c106e659e', // row.id,
          hospitalId: row.enterpriseId, // '18764', // row.enterpriseId,
          distributorId: row.supplierId, // 'dc31b2d3d7784667b47144c243882f4f', // row.supplierId,
          authState: row.authState
        }
      })
    },
    //   查看审核历史
    lookHistory (row) {
      this.isShowHistory = true
      this.historyLoading = true
      this.$axios({
        method: 'get',
        url: authSupplierHospitalSelectcAcAuditRecord,
        params: {
          authId: row.id,
          authType: 1
        }
      })
        .then(res => {
          if (hasIn(res, ['data', 'payload'])) {
            this.historyDataList = res.data.payload || []
            this.historyLoading = false
          }
        })
        .catch(() => {
          this.historyLoading = false
        })
    },
    // 每次点击编辑详情的时候调用全部删除临时表数据
    deleteAll (id) {
      this.$axios({
        method: 'post',
        url: commonPcAdmittanceTemporarydeleteAll,
        data: {
          companyId: this.$store.state.companyId,
          hosId: id
        }
      }).then(res => {
        console.log(res, 'res')
      })
    },
    // 编辑
    edit (row) {
      this.deleteAll(row.enterpriseId)
      let objnameid = this.dataList.find(item => {
        return item.id === row.id
      })
      this.$router.push({
        path: '/distributorSide/distributionAdmittance/applyAdmittance',
        query: {
          filgtype: '9', // 标记编辑，再提交状态
          id: row.id,
          hospitalId: row.enterpriseId,
          distributorId: row.supplierId,
          name: objnameid.enterpriseName, // 医院名称
          ids: objnameid.enterpriseId // 医院id
        }
      })
    },
    // 搜索功能-点击查询
    searchForm (condition) {
      this.searchData = condition
      this.getTableList()
    },
    // 点击重置
    resetForm (condition) {
      this.curPage = 1
      this.searchData = condition
      this.getTableList()
    },
    // 页码更改
    handleSizeChange (size) {
      this.pageSize = size
      this.curPage = 1
      this.getTableList()
    },
    // 切换不一样的页面,重置一切数据
    handleCurrentChange (curPage) {
      this.curPage = curPage
      this.getTableList()
    },
    // 申请准入
    toAdd () {
      this.$router.push({
        path: '/distributorSide/distributionAdmittance/applyAdmittance'
      })
    },
    _initGlobalBtn () {
      let obj = {
        type: 'btn',
        arr: [{ type: 'primary', val: '申请准入', handle: this.toAdd }]
      }
      this.$store.commit('changeLayoutHandle', obj)
    }
  }
}
</script>
<style lang="less">
.custom-oneline {
  text-overflow: ellipsis;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 1;
  // 下面两个有区别
  // word-wrap: break-word;
  word-break: break-all;

  /* autoprefixer: ignore next */
  -webkit-box-orient: vertical;
  cursor: pointer;
}
.channel-management-distribution-admittance-box {
  .template-box {
    height: 100%;
    overflow: auto;
    .pagination {
      padding-top: 10px;
      display: flex;
      justify-content: flex-end;
      padding-right: 0;
    }
  }
}
</style>
